using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

/// <summary>
/// 硬件设备性能适配工具
/// </summary>
public static class DevicePerformanceUtil
{
    static int width, height;
    public static void Initial()
    {
        width = Screen.width;
        height = Screen.height;
    }
    public static QualityLevel qualityLevel = QualityLevel.High;
    /// <summary>
    /// 获取设备性能评级
    /// </summary>
    /// <returns>性能评级</returns>
    public static DevicePerformanceLevel GetDevicePerformanceLevel()
    {
        //显存
        int graphicsMemorySize = SystemInfo.graphicsMemorySize;
        //内存
        int systemMemorySize = SystemInfo.systemMemorySize;
        int pf = SystemInfo.processorFrequency;
#if UNITY_EDITOR
        return DevicePerformanceLevel.High;
#elif UNITY_UNITY_STANDALONE_WIN
        if (SystemInfo.graphicsDeviceVendorID == 32902)
        {
            //集显
            return DevicePerformanceLevel.Mid;
        }
        return DevicePerformanceLevel.High;
#elif UNITY_IPHONE || UNITY_IOS
            if (graphicsMemorySize >= 2000 && systemMemorySize >= 4000)
                    return DevicePerformanceLevel.High;
                else if (graphicsMemorySize >= 1000 && systemMemorySize >= 2000)
                    return DevicePerformanceLevel.Mid;
                else
                    return DevicePerformanceLevel.Low;
#elif UNITY_ANDROID
             if (graphicsMemorySize >= 2000 && systemMemorySize >= 6000&& pf>=2400)
                    return DevicePerformanceLevel.High;
                else if (graphicsMemorySize >= 1000 && systemMemorySize >= 4000)
                    return DevicePerformanceLevel.Mid;
                else
                    return DevicePerformanceLevel.Low;
#else
         return DevicePerformanceLevel.High;
#endif

    }

    /// <summary>
    /// 根据手机性能修改项目设置
    /// </summary>
    /// <param name="lowQuality">QualitySettings中对应Low的等级</param>
    /// <param name="midQuality">QualitySettings中对应Mid的等级</param>
    /// <param name="highQuality">QualitySettings中对应High的等级</param>
    public static void ModifySettingsBasedOnPerformance(int lowQuality, int midQuality, int highQuality)
    {
        DevicePerformanceLevel level = GetDevicePerformanceLevel();
        switch (level)
        {
            case DevicePerformanceLevel.Low:
                QualitySettings.SetQualityLevel(lowQuality, true);
                break;
            case DevicePerformanceLevel.Mid:
                QualitySettings.SetQualityLevel(midQuality, true);
                break;
            case DevicePerformanceLevel.High:
                QualitySettings.SetQualityLevel(highQuality, true);
                break;
        }
    }

    /// <summary>
    /// 根据机型配置自动设置质量
    /// </summary>
    public static DevicePerformanceLevel ModifySettingsBasedOnPerformance()
    {
        
        DevicePerformanceLevel level = GetDevicePerformanceLevel();
        switch (level)
        {
            case DevicePerformanceLevel.Low:
                SetQualitySettings(QualityLevel.Low);
                break;
            case DevicePerformanceLevel.Mid:
                SetQualitySettings(QualityLevel.Mid);
                break;
            case DevicePerformanceLevel.High:
                SetQualitySettings(QualityLevel.High);
                break;
        }
        return level;
    }

    /// <summary>
    /// 供LUA模块使用
    /// 根据自身需要调整各级别需要修改的设置，可根据需求修改低中高三种方案某一项具体设置
    /// </summary>
    /// <param name="qualityLevel">质量等级</param>
    public static void SetQualitySettingsByLua(int value)
    {
#if UNITY_ANDROID
        if (PlatformTool.Instance.IsEmulator())
        {
            QualitySettings.SetQualityLevel(3);
            return;
        }
#endif
        QualityLevel level = (QualityLevel)(value + 1);//QualityLevel.noQuality;

        switch (level)
        {
            case QualityLevel.Low:
                QualitySettings.SetQualityLevel(1);
#if UNITY_ANDROID
                Application.targetFrameRate = 30;
#endif
                qualityLevel = QualityLevel.Low;
                break;
            case QualityLevel.Mid:
                QualitySettings.SetQualityLevel(2);
#if UNITY_ANDROID
                Application.targetFrameRate = 30;
#endif
                qualityLevel = QualityLevel.Mid;
                break;
            case QualityLevel.High:
                QualitySettings.SetQualityLevel(3);
#if UNITY_ANDROID
                Application.targetFrameRate = 45;
#endif
                qualityLevel = QualityLevel.High;
                break;
        }
    }

    /// <summary>
    /// 根据自身需要调整各级别需要修改的设置，可根据需求修改低中高三种方案某一项具体设置
    /// </summary>
    /// <param name="qualityLevel">质量等级</param>
    public static void SetQualitySettings(QualityLevel qLevel)
    {
        switch(qLevel)
        {
            case QualityLevel.Low:
                QualitySettings.SetQualityLevel(1);
#if UNITY_ANDROID
                Application.targetFrameRate = 30;
#endif
                qualityLevel = QualityLevel.Low;
                break;
            case QualityLevel.Mid:
                QualitySettings.SetQualityLevel(2);
#if UNITY_ANDROID
                Application.targetFrameRate = 30;
#endif
                qualityLevel = QualityLevel.Mid;
                break;
            case QualityLevel.High:
                QualitySettings.SetQualityLevel(3);
#if UNITY_ANDROID
                Application.targetFrameRate = 45;
#endif
                qualityLevel = QualityLevel.High;
                break;
        }
    }
    public static void SetResolutionLevel(int level)
    {
        int MaxPix = 2560;
        switch (level)
        {
            case 0:
                MaxPix = 1280;
                break;
            case 1:
                MaxPix = 1680;
                break;
            case 2:
                MaxPix = 2160;
                break;
            case 3:
                MaxPix = 2560;
                break;
            case 4:
                MaxPix = 3000;
                break;
        }
        float fw = width;
        float fh = height;
        float mw = MaxPix;
        float s = mw / fw;
        if (s < 1)
        {
            int cw = (int)(fw * s);
            int ch = (int)(fh * s);
            Screen.SetResolution(cw, ch, FullScreenMode.FullScreenWindow);
        }
        else
        {
            Screen.SetResolution(width, height, FullScreenMode.FullScreenWindow);
        }
    }
    public static void SetResolutionRatio(float ratio)
    {
        float fw = width;
        float fh = height;
        int cw = (int)(fw * ratio);
        int ch = (int)(fh * ratio);
        Screen.SetResolution(cw, ch, FullScreenMode.FullScreenWindow);
    }
}
public enum DevicePerformanceLevel
{
    Low=1,
    Mid=2,
    High=3
}
public enum QualityLevel
{
    noQuality=0,
    Low=1,
    Mid=2,
    High=3
}